home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 102_01.zip / HANGMAN.C < prev    next >
Text File  |  1993-06-03  |  4KB  |  216 lines

  1. /*
  2.     The game of "Hangman"
  3.  
  4.     Modified for BDS C by Leor Zolman, 2/82
  5.  
  6.     The dictionary is simply a text file of words and/or phrases,
  7.     one word or phrase per line. Punctuation and spaces are OK; the
  8.     program will display all punctuation and non-alphabetic characters
  9.     of the string. You only have to guess the letters.
  10.  
  11.     Usage:
  12.         A>hangman [dictname]
  13.  
  14.     If no dictionary name is given, the default HANGMAN.WDS is presumed.
  15. */
  16.  
  17. #include <bdscio.h>
  18.  
  19. #define MAXLEN 130
  20. #define DICT "hangman.wds"
  21. #define MAXERR 7
  22. #define MINSCORE 0
  23. #define MINLEN 7
  24. #define stderr 4
  25.  
  26. char *dictfile;
  27. int alive,lost;
  28. char dict[BUFSIZ];
  29. int dictlen;
  30. char *wordlist[1000];
  31. int errors, words;
  32. char firstguess;
  33.  
  34. main(argc,argv) char **argv;
  35. {
  36.     int i;
  37.  
  38.     alive = lost = dictlen = errors = words = 0;
  39.  
  40.     if(argc==1) dictfile=DICT;
  41.     else dictfile=argv[1];
  42.  
  43.     setup();
  44.     printf("Read %d words from file %s\n\n",dictlen,dictfile);
  45.  
  46.  
  47.     for(;;)
  48.     {    startnew();
  49.         while(alive>0)
  50.         {    stateout();
  51.             getguess();
  52.         }
  53.         words=words+1;
  54.         if(lost) wordout();
  55.         else youwon();
  56.         pscore();
  57.     }
  58. }
  59.  
  60. setup()
  61. {
  62.     srand1("Ready to play hangman?");
  63.     getchar(); putchar('\n');
  64.  
  65.     if((fopen(dictfile,dict))==ERROR) fatal("No dictionary");
  66.     dictlen=readin();
  67.     fclose(dict);
  68.     words = 0;
  69. }
  70.  
  71. int readin()        /* read in dictionary, count up words */
  72. {
  73.     int i;
  74.     int count;
  75.     char *wptr;
  76.     char linebuf[MAXLINE];
  77.  
  78.     count = 0;    
  79.     while (fgets(linebuf,dict))
  80.     {
  81.         if ((wptr = sbrk(strlen(linebuf)+1)) == ERROR)
  82.             break;
  83.         wordlist[count] = wptr;
  84.         for (i = 0; linebuf[i] != '\n'; i++)
  85.             wptr[i] = linebuf[i];
  86.         wptr[i] = '\0';
  87.         count++;
  88.     }
  89.     return count;
  90. }
  91.  
  92. char word[MAXLEN], alph[MAXLEN], realword[MAXLEN];
  93.  
  94. startnew()
  95. {    int i;
  96.     for(i=0; i<MAXLEN; i++) word[i] = alph[i] = realword[i] = 0;
  97.     getword();
  98.     alive=MAXERR;
  99.     lost=0;
  100.     firstguess = 1;
  101. }
  102.  
  103. stateout()
  104. {    int i;
  105.     char noneflag;
  106.  
  107.     noneflag = 1;
  108.     for(i=0;i<26;i++)
  109.         if(alph[i]!=0) {
  110.             if (noneflag) {
  111.                 printf("\nGuesses: ");
  112.                 noneflag = 0;
  113.             }
  114.             putchar(alph[i]);
  115.         }
  116.  
  117.     if (firstguess) {
  118.         printf("\nNew word: %s ",word);
  119.         firstguess = 0;
  120.     }
  121.     else
  122.         printf("\nWord: %s ",word);
  123.  
  124.     printf("\nerrors: %d/%d\n",MAXERR-alive,MAXERR);
  125. }
  126.  
  127. getguess()
  128. {    char c;
  129.     int ok,i;
  130.  
  131.     ok = 0;
  132. loop:
  133.     printf("Guess: ");
  134.  
  135.     
  136.     c = tolower(getchar());
  137.  
  138.     if (c < 'a' || c > 'z') {
  139.         puts("\nGuesses are letters between a and z.\n");
  140.         goto loop;
  141.     } else putchar('\n');
  142.  
  143.     if(alph[c-'a']!=0)
  144.     {    printf("You guessed that letter already.\n");
  145.         goto loop;
  146.     }
  147.     else alph[c-'a']=c;
  148.  
  149.     for(i=0;realword[i]!=0;i++)
  150.         if(tolower(realword[i])==c)
  151.         {    word[i]=realword[i];
  152.             ok=1;
  153.         }
  154.  
  155.     if(ok==0)
  156.     {    alive--;
  157.         errors=errors+1;
  158.         if(alive<=0) lost=1;
  159.         return;
  160.     }
  161.     for(i=0;word[i]!=0;i++)
  162.         if(word[i]=='.') return;
  163.     alive=0;
  164.     lost=0;
  165.     return;
  166. }
  167.  
  168. wordout()
  169. {
  170.     errors=errors+2;
  171.     printf("The answer was %s, you BLEW it!\n",realword);
  172. }
  173.  
  174. youwon()
  175. {
  176.     printf("\7You win, the word is:  %s\n",realword);
  177. }
  178.  
  179. fatal(s) char *s;
  180. {
  181.     fprintf(stderr,"%s\n",s);
  182.     exit(1);
  183. }
  184.  
  185. getword()
  186. {    int i,j;
  187.     char c;
  188.  
  189.     if (dictlen == 0) {
  190.         printf("All available words have been used. Bye!\n");
  191.         exit(0);
  192.     }
  193.  
  194.     j = rand() % dictlen;
  195.     strcpy(realword,wordlist[j]);
  196.     wordlist[j] = wordlist[dictlen-1];
  197.     dictlen--;        
  198.  
  199.     for(j=0; j<strlen(realword); j++)
  200.         if ((c = tolower(realword[j])) < 'a' || c > 'z')
  201.             if (c == '.')    word[j] = '.' + 0x80;
  202.             else        word[j] = c;
  203.         else
  204.             word[j] = '.';
  205. }
  206.  
  207. pscore()
  208. {
  209.     if(words!=0)
  210.         printf("You average number of errors for %d word%s is %d.%d\n",
  211.         words,
  212.         words == 1 ? "" : "s",
  213.         errors/words,
  214.         (errors * 100 / words) % 100);
  215. }
  216.